﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTunesLib;
using System.ComponentModel;
using System.Collections.ObjectModel;
using myTunes.Commands;

namespace myTunes.BO
{
    public class PlaylistViewModel : INotifyPropertyChanged
    {
        private IITPlaylist _playlist;
        private ObservableCollection<TrackViewModel> _tracksViewModel;
        private iTunesPlayer _iTunesPlayer;
        private PlaylistViewModelNextTrackCommand _nextTrackCommand;
        private PlaylistViewModelPreviousTrackCommand _previousTrackCommand;
        private PlaylistViewModelSetShuffleOnOffCommand _setShuffleOnOffCommand;
        private PlaylistViewModelSetRepeatOnOffCommand _setRepeatOnOffCommand;        
        private TrackViewModel _currentTrack;

        public TrackViewModel CurrentTrack
        {
            get 
            {
                return _currentTrack;
            }
            internal set
            {
                _currentTrack = value;
                RaisePropertyChanged("CurrentTrack");
            }
        }

        public int PlaylistID
        {
            get
            {
                return _playlist.playlistID;
            }
        }

        public PlaylistViewModelPreviousTrackCommand PreviousTrackCommand
        {
            get { return _previousTrackCommand; }
        }

        public PlaylistViewModelNextTrackCommand NextTrackCommand
        {
            get { return _nextTrackCommand; }
        }

        public PlaylistViewModelSetShuffleOnOffCommand SetShuffleOnOffCommand
        {
            get { return _setShuffleOnOffCommand; }
        }

        #region Properties

        public ObservableCollection<TrackViewModel> TracksViewModel
        {
            get 
            {
                if (_tracksViewModel == null)
                    RefreshTracks();
                return _tracksViewModel; 
            }
        }

        public int Duration
        {
            get
            {
                return _playlist.Duration;
            }
        }

        public int Index
        {
            get
            {
                return _playlist.Index;
            }
        }

        public string Name 
        {
            get
            {
                return _playlist.Name;
            }
        }

        public int Id 
        {
            get
            {
                return _playlist.playlistID;
            }
        }

        public bool IsShuffle 
        {
            get
            {
                return _playlist.Shuffle;
            }
            set
            {
                _playlist.Shuffle = value;
                RaisePropertyChanged("IsShuffle");
            }
        }

        public bool IsRepeat
        {
            get
            {
                return _playlist.SongRepeat != ITPlaylistRepeatMode.ITPlaylistRepeatModeOff;
            }
            set
            {
                _playlist.SongRepeat = value? ITPlaylistRepeatMode.ITPlaylistRepeatModeAll: ITPlaylistRepeatMode.ITPlaylistRepeatModeOff;
                RaisePropertyChanged("IsRepeat");
            }
        }

        public double Size 
        {
            get
            {
                return _playlist.Size;
            }
        }
        
        public bool IsSongRepeat 
        {
            get
            {
                if (_playlist.SongRepeat == null)
                    return false;

                return _playlist.SongRepeat.ToString() != "ITPlaylistRepeatModeOff";
            }
        }

        public TimeSpan Time 
        {
            get
            {
                TimeSpan ts;
                string timeStr = _playlist.Time;
                if (timeStr.Split(':').Length <= 2)
                {
                    //<1 day
                    timeStr = timeStr.Insert(0, "00:");
                }

                TimeSpan.TryParse(timeStr, out ts);
                return ts;
            }
        }

        public bool IsVisible 
        {
            get
            {
                return _playlist.Visible;
            }
        }

        #endregion

        public PlaylistViewModel(IITPlaylist playlist, iTunesPlayer player)
        {
            if (player == null)
                throw new Exception("iTunesPlayer cannot be null");
            _playlist = playlist;
            _iTunesPlayer = player;
            
            _iTunesPlayer.CurrentTrackChanged += new iTunesPlayer.CurrentTrackChangedHandler(ITunesPlayer_CurrentTrackChanged);
            _nextTrackCommand = new PlaylistViewModelNextTrackCommand(this);
            _previousTrackCommand = new PlaylistViewModelPreviousTrackCommand(this);
            _setShuffleOnOffCommand = new PlaylistViewModelSetShuffleOnOffCommand(this);
            _setRepeatOnOffCommand = new PlaylistViewModelSetRepeatOnOffCommand(this);
        }

        void ITunesPlayer_CurrentTrackChanged(TrackViewModel newTrack)
        {
            RaisePropertyChanged("CurrentTrack");
        }

        public void PlayFirstTrack()
        {
            _playlist.PlayFirstTrack();
            UpdateCurrentTrack();
        }

        public void PlayNextTrack()
        {
            _iTunesPlayer._iTunesApi.NextTrack();
            UpdateCurrentTrack();
        }

        public void PlayPreviousTrack()
        {
            _iTunesPlayer._iTunesApi.PreviousTrack();
            UpdateCurrentTrack();
        }

        private void UpdateCurrentTrack()
        {
            /*
            IITTrack track = _iTunesPlayer._iTunesApi.GetCurrentTrack();
            if (track != null)
            {
                foreach (TrackViewModel tvm in _tracksViewModel)
                {
                    if (tvm.TrackID == track.trackID)
                    {
                        CurrentTrack = tvm;
                        CurrentTrack.StartBroadcastingTrackInfo();
                        break;
                    }
                }
            }
            else
                CurrentTrack = null;*/
        }

        private void RefreshTracks()
        {
            _tracksViewModel = new ObservableCollection<TrackViewModel>();
            foreach (IITTrack track in _playlist.Tracks)
            {
                _tracksViewModel.Add(new TrackViewModel(track, _iTunesPlayer));
            }
            RaisePropertyChanged("TracksViewModel");
        }

        private void RaisePropertyChanged(string propertyName)
        {
            if(PropertyChanged!=null)
                PropertyChanged(this,new PropertyChangedEventArgs(propertyName));
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
